<?php
/**
  * NOAA Weather Class
  * v.000000001
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  *
  * -----------------------------------------------------------
  *
  * Copyright 2006 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */

/**
 * Requires the nusoap class
 * get it at
 * http://dietrich.ganx4.com/nusoap/
 */
require_once('nusoap.php');


class NOAA_weather{

/**
 * @var string noaa_soap_url the url to the NOAA soap service 
 */
var $noaa_soap_url= "http://weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl";

/**
 * function to instantiate the class
 */
function NOAA_weather()
{
}

function get_weather($_lat, $_lng,$_date)
{
	$timeout = 43200*0;
	$dest_file = '/tmp/noaa_weather_'.md5($_lat.$_lng.$_date);
	if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout))
      	{
	$lat= $_lat;
	$lng= $_lng;
	$numdays = 5;
	$product= 'time-series';
	$format= '24 hourly';
	$startdate = $_date;
	$result = $this->NDFDgenByDay( $lat,$lng,$product,$startdate,$numdays,$format);

	$temp[] = $this->simplexml2ISOarray($result->data->parameters->temperature->value);
	$data["maxtemp"]=  $this->simplexml2ISOarray($result->data->parameters->temperature);
	$data["maxtemp"]=  $data["maxtemp"]["value"];
	$data["maxtemp"] = array_merge($temp,$data["maxtemp"]);
	if ($data["maxtemp"][0]=="")array_shift($data["maxtemp"]);
        $temp = null;

	$temp[] = $this->simplexml2ISOarray($result->data->parameters->temperature[1]->value);
	$data["mintemp"]=  $this->simplexml2ISOarray($result->data->parameters->temperature[1]);
	$data["mintemp"]=  $data["mintemp"]["value"];
	$data["mintemp"] = array_merge($temp,$data["mintemp"]);
        $temp = null;

	$temp[] = $this->simplexml2ISOarray($result->data->parameters->precipitation->value);
	$data["precip2"] = $this->simplexml2ISOarray($result->data->parameters->precipitation);
	$data["precip2"] = $data["precip2"]["value"];
	$data["precip2"] = array_merge($temp,$data["precip2"]);
	$data["precip"][0] = $data["precip2"][0]+$data["precip2"][5]/2;
	$data["precip"][1] = $data["precip2"][1]+$data["precip2"][6]/2;
	$data["precip"][2] = $data["precip2"][2]+$data["precip2"][7]/2;
	$data["precip"][3] = $data["precip2"][3]+$data["precip2"][8]/2;
	$data["precip"][4] = $data["precip2"][4]+$data["precip2"][9]/2;
	$temp = null;

	//$data["icons"] = 
	$temp[]= $this->simplexml2ISOarray($result->data->parameters->conditions_icon->icon_link);
	$data["icons"] = $this->simplexml2ISOarray($result->data->parameters->conditions_icon);
	$data["icons"] = $data["icons"]["icon_link"];
	$data["icons"] = array_merge($temp,$data["icons"]);
	$temp = null;
	$temp[]=  $this->simplexml2ISOarray($result->data->parameters->weather->conditions);
	$data["conditions"]=$this->simplexml2ISOarray($result->data->parameters->weather);
	$data["conditions"] = $data["conditions"]["conditions"];
	$data["conditions"] = array_merge($temp,$data["conditions"]);
	foreach ( $data["conditions"] as $weather)
	{
	$data["summary"][] = $weather["@"]["weather-summary"];
	}
	$data["conditions"] =null;
	file_put_contents($dest_file, serialize($data));
	}
	else{
		$data= unserialize(file_get_contents($dest_file));
	}
	return $data;
}

function NDFDgenByDay( $_latitude,$_longitude,$_product,$_startDate,$_numdays,$_format)
{
	$xml = false;
	$parameters = array('latitude' => $_latitude,
		'longitude'  => $_longitude,
		'product' => $_product,
		'startDate' => $_startDate,
		'numDays' => $_numdays,
		'format' => $_format);
	$soapclient = new soapclient($this->noaa_soap_url,true);
	$err = $soapclient->getError();
	if ($err) {
		echo('Unable to make SOAP call!');
		print_r($err);
	}
	$result = $soapclient->call('NDFDgenByDay',$parameters);
	if ($soapclient->fault) {
		$err= 'No data available for this area';
		echo $err;
	} else {
		$result=str_replace("probability-of-precipitation","precipitation",$result);
		$result=str_replace("weather-conditions","conditions",$result);
		$result=str_replace("conditions-icon","conditions_icon",$result);
		$result=str_replace("icon-link","icon_link",$result);
		$xml = new SimpleXMLElement($result);
		$result = $xml;
	}
	return $result;
}

//http://us2.php.net/manual/en/ref.simplexml.php#68286
function simplexml2ISOarray($xml) {
   if (get_class($xml) == 'SimpleXMLElement') {
       $attributes = $xml->attributes();
       foreach($attributes as $k=>$v) {
           if ($v) $a[$k] = (string) $v;
       }
       $x = $xml;
       $xml = get_object_vars($xml);
   }
   if (is_array($xml)) {
       if (count($xml) == 0) return (string) $x; // for CDATA
       foreach($xml as $key=>$value) {
           if($key != '@attributes') {
               $r[$key] = $this->simplexml2ISOarray($value);
               if ( !is_array( $r[$key] ) ) $r[$key] = utf8_decode( $r[$key] );
           }
       }
       if (isset($a)) $r['@'] = $a; // Attributes
       return $r;
   }
   return (string) $xml;
}

}




?>
